package com.twu.biblioteca;

import java.util.ArrayList;

public class Library {
    ArrayList<RentalItem> rentalItems;
    ArrayList<Customer> customers;
    RentedItems rentedItems;
    int counterIDForItems = 11111;

    Library() {
        rentalItems = new ArrayList<RentalItem>();
        customers = new ArrayList<Customer>();
        rentedItems = new RentedItems();
    }

    public int addItem(RentalItem rentalItem) {
        rentalItem.setID(counterIDForItems);
        counterIDForItems += 1;
        rentalItems.add(rentalItem);
        return rentalItem.getID();

    }


    public int getItemsCount() {
        return rentalItems.size();
    }

    public int getCustomerCount() {
        return customers.size();
    }

    public String registerNewCustomer(Customer customer) {

        customers.add(customer);
        return customer.username;
    }

    public int getCountOfCheckedOutObjects() {
        return rentedItems.getSize();
    }

    public int rentRentalObject(String username, int itemName) throws Exception {
        Customer customer = findCustomer(username);
        RentalItem rentalItem = findRentalObjectByID(itemName);
        rentedItems.borrow(customer, rentalItem);
        return rentalItem.getID();
    }


    public Customer findCustomer(String username) throws Exception {
        for (Customer customer : customers) {
            if (customer.username.equals(username))
                return customer;
        }
        throw new CustomerDoesNotExist();
    }

    public RentalItem findRentalObjectByID(int objectIDToRent) throws Exception {

        for (RentalItem rentalItem : rentalItems) {
            if (rentalItem.getID() == objectIDToRent)
                return rentalItem;
        }
        throw new InvalidRentalObject();
    }

    public int returnBorrowedItem(String username, int itemToReturn) throws Exception {
        Customer customer = findCustomer(username);
        RentalItem rentalItem = findRentalObjectByID(itemToReturn);
        rentedItems.returnItem(customer, rentalItem);
        return rentalItem.getID();


    }

    public String seeCheckedOutList() {
        return rentedItems.seeBorrowedItems();
    }

    public String seeAllItems(Boolean isBook) {
        StringBuilder itemListAsString = new StringBuilder();


        if (isBook)
            for (RentalItem item : rentalItems) {
                if (item.getClass().toString().equals("class com.twu.biblioteca.Book"))
                    itemListAsString.append(item.getDescription());
            }
        else
            for (RentalItem item : rentalItems) {

                if (item.getClass().toString().equals("class com.twu.biblioteca.Video"))
                    itemListAsString.append(item.getDescription());
            }


        return itemListAsString.toString();
    }

    public String seeAllAvailableItems(Boolean isBook) {
        StringBuilder itemListOfAvailableItems = new StringBuilder();

        if (isBook)
            for (RentalItem item : rentalItems) {
                if (item.getClass().toString().equals("class com.twu.biblioteca.Book")&&!rentedItems.isItemAlreadyRented(item))
                    itemListOfAvailableItems.append(item.getDescription());
            }
        else
            for (RentalItem item : rentalItems) {

                if (item.getClass().toString().equals("class com.twu.biblioteca.Video")&&!rentedItems.isItemAlreadyRented(item))
                    itemListOfAvailableItems.append(item.getDescription());
            }

        return itemListOfAvailableItems.toString();
    }

}
